package jp.idahopotato.activity;

import jp.matome.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class OmamoriSetActivity extends Activity {
	private static final String FILE_NAME = "PreferenceOmamoriSetFile";
	CreateProductHelper helper = null;
	SQLiteDatabase db = null;
	final RelativeLayout rl[] = new RelativeLayout[2];
	final RelativeLayout skill_layout[] = new RelativeLayout[2];
	private String skill_ID[] = {"-1","-1"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.omamori);
		final SharedPreferences pref = getSharedPreferences(FILE_NAME, MODE_PRIVATE);
		rl[0] = (RelativeLayout) findViewById(R.id.o_skill_1);
		rl[1] = (RelativeLayout) findViewById(R.id.o_skill_2);

		final RadioButton o_wep0 = (RadioButton)findViewById(R.id.o_wep0);
		final RadioButton o_wep1 = (RadioButton)findViewById(R.id.o_wep1);
		final RadioButton o_wep2 = (RadioButton)findViewById(R.id.o_wep2);
		final RadioButton o_wep3 = (RadioButton)findViewById(R.id.o_wep3);

		//全画面からの文字列を取得
		Intent data = getIntent();
		Bundle extras = data.getExtras();
		String str = extras.getString("SELECT_MENU");
		TextView title = (TextView)findViewById(R.id.text_omamori);
		title.setText(str);

		int o_wep = pref.getInt("o_wep", 0);
		if(o_wep == 0){
			o_wep0.setChecked(true);
		} else if(o_wep == 1){
			o_wep1.setChecked(true);
		} else if(o_wep == 2){
			o_wep2.setChecked(true);
		} else if(o_wep == 3){
			o_wep3.setChecked(true);
		}

		//設定してあったスキル条件の表示
		LayoutInflater mInflater = getLayoutInflater();
		helper = new CreateProductHelper(OmamoriSetActivity.this);

		String skill_1 = pref.getString("skill_1", "-1,0");
		String skill_2 = pref.getString("skill_2", "-1,0");
		String set[][] = new String[2][];
		set[0] = skill_1.split(",");
		set[1] = skill_2.split(",");
		try{
			//データベースを開く
			db = helper.getReadableDatabase();

			//各グループのスキル名の取得と、子リストの表示
			String columns[] = {"skill_id", "skill_name"};
			Cursor cursor;
			//アダプタ登録用全リスト
			SQLiteQueryBuilder build = new SQLiteQueryBuilder();
			build.setTables("m_skill");
			//検索条件はスキルID
			String where = "m_skill.skill_id = ?";
			for(int i=0; i<2; i++) {
				final int temp_index = i;
				String[] param = {set[i][0]};
				String sql = build.buildQuery(columns, where, param, null, null, "skill_id ASC", null);
				cursor = db.rawQuery(sql, param);

				skill_layout[i] = (RelativeLayout) mInflater.inflate(R.layout.omamoriset, null);
				final TextView skillName = (TextView)skill_layout[i].findViewById(R.id.o_skill_name);
				if(!set[i][0].equals("-1") && cursor.moveToFirst()){
					skill_ID[i] = cursor.getString(0);
					skillName.setText(cursor.getString(1));
				} else {
					skill_ID[i] = "-1";
					skillName.setText("未設定");
				}
				int index = Integer.parseInt(set[i][1]);
				final Spinner point = (Spinner) skill_layout[i].findViewById(R.id.o_skill_point);
				point.setSelection(15-index);

				Button changeBtn = (Button) skill_layout[i].findViewById(R.id.o_changeButton);
				changeBtn.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// TODO 自動生成されたメソッド・スタブ
						Intent intent = new Intent(OmamoriSetActivity.this, SelectSkillGroupActivity.class);
						intent.putExtra("SELECT_SKILL", "お守り設定");
						intent.putExtra("NUM", ""+temp_index);
						startActivityForResult(intent, 0);
					}
				});

				Button deleteBtn = (Button)skill_layout[i].findViewById(R.id.o_deleteButton);
				deleteBtn.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// TODO 自動生成されたメソッド・スタブ
						skill_ID[temp_index] = "-1";
						skillName.setText("未設定");
						point.setSelection(15);
					}
				});

				rl[i].addView(skill_layout[i]);
			}
		} catch(Exception e){
			Log.e("ERROR", e.toString());
		}
		db.close();

		//設定したお守り条件を確定して前画面に戻る
		Button backBtn = (Button)findViewById(R.id.backbutton_omamori);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SharedPreferences.Editor editor = pref.edit();

				if(o_wep0.isChecked()){
					editor.putInt("o_wep", 0);
				} else if(o_wep1.isChecked()){
					editor.putInt("o_wep", 1);
				} else if(o_wep2.isChecked()){
					editor.putInt("o_wep", 2);
				} else if(o_wep3.isChecked()){
					editor.putInt("o_wep", 3);
				}

				String str = "";
				for(int i=0; i<2; i++){
					if(!skill_ID[i].equals("-1")){
						Spinner point = (Spinner)skill_layout[i].findViewById(R.id.o_skill_point);
						String data = skill_ID[i] + "," + (String)point.getSelectedItem();
						str += data+"/";
						editor.putString("skill_"+(i+1), data);
					} else {
						str += "-1,0/";
						editor.putString("skill_"+(i+1), "-1,0");
					}
				}
				Log.v("selected", "お守りセット" + str);
				editor.commit();

				Intent intent = getIntent();
				intent.putExtra("RETURN_FLAG", "お守り設定から");
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data != null && data.hasExtra("RETURN_FLAG")){
			Bundle extras = data.getExtras();
			int index = Integer.parseInt(extras.getString("RETURN_ID"));
			skill_ID[index] = extras.getString("SKILL_ID");
			TextView skillName = (TextView)skill_layout[index].findViewById(R.id.o_skill_name);
			skillName.setText(extras.getString("SKILL_NAME"));
		}
	}

}
